package com.sinodata.bsm.common.utils;

/**
 * 
 * <p>
 * Description: 加密算法类
 * </p>
 *
 * @author liulibo
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-5-18 下午2:43:59    liulibo        1.0         To create
 * </p>
 *
 * @since 
 * @see
 */
public class Encipher {

    public Encipher() {

    }

    /**
     * 将明文加密成密文
     * 
     * @param strPasswd
     *            待加密明文
     * @return 返回密文
     */
    public static String EncodePasswd(String strPasswd) {
        String strEncodePasswd = new String("");
        String des = new String();
        String strKey = new String();
        if ((strPasswd == null) | (strPasswd.length() == 0)) {
            strEncodePasswd = "";
            return strEncodePasswd;
        }
        strKey = "zxcvbnm,./asdfghjkl;'qwertyuiop[]\\1234567890-=` ZXCVBNM<>?:LKJHGFDSAQWERTYUIOP{}|+_)(*&^%$#@!~";
        for (; strPasswd.length() < 8; strPasswd = strPasswd + '\001')
            ;
        des = "";
        for (int n = 0; n <= strPasswd.length() - 1; n++) {
            char code;
            char mid;
            do {
                for (code = (char) (int) Math.rint(Math.random() * 100D); code > 0 && ((code ^ strPasswd.charAt(n)) < 0 || (code ^ strPasswd.charAt(n)) > 90); code--)
                    ;
                mid = '\0';
                int flag = code ^ strPasswd.charAt(n);
                if (flag > 93)
                    mid = '\0';
                else
                    mid = strKey.charAt(flag);
            } while (!((code > '#') & (code < 'z') & (code != '|') & (code != '\'') & (code != ',') & (mid != '|') & (mid != '\'') & (mid != ',')));
            char temp = '\0';
            temp = strKey.charAt(code ^ strPasswd.charAt(n));
            des = des + code + temp;
        }
        strEncodePasswd = des;
        return strEncodePasswd;
    }

    /**
     * 将密文解密成明文
     * 
     * @param varCode
     *            待解密密文
     * @return 返回解密后原文
     */
    public static String DecodePasswd(String varCode) {
        String des = new String();
        String strKey = new String();
        if (varCode == null || varCode.length() == 0)
            return "";
        strKey = "zxcvbnm,./asdfghjkl;'qwertyuiop[]\\1234567890-=` ZXCVBNM<>?:LKJHGFDSAQWERTYUIOP{}|+_)(*&^%$#@!~";
        if (varCode.length() % 2 == 1)
            varCode = varCode + "?";
        des = "";
        int n;
        for (n = 0; n <= varCode.length() / 2 - 1; n++) {
            char b = varCode.charAt(n * 2);
            int a = strKey.indexOf(varCode.charAt(n * 2 + 1));
            des = des + (char) (b ^ a);
        }
        n = des.indexOf(1);
        if (n > 0) {
            return des.substring(0, n);
        } else {
            return des;
        }
    }

    /**
     * 用于测试
     * 
     * @param args
     */
    public static void main(String[] args) {
        String password = "a";// 请换成待加密的明文
        String encCode = ".ON};:8?(8R)W&[#";// 请换成需要解密的密文
        System.out.println(password + "的密文是：" + EncodePasswd(password));
        System.out.println(encCode + "的原文是：" + DecodePasswd(encCode));
    }

}